﻿using UnityEngine;
using System.Collections;

public class Champ : MonoBehaviour {
	Controller controller;
	public GameObject descr;
	GameObject descripcion;
	bool sube = true;
	private AudioSource audioSource;
	private bool auxG = true;

	
	private int estadomouse =1;

		
	

	// Use this for initialization
	void Start () {
		controller= GameObject.Find("Controller").GetComponent<Controller>();
		audioSource = gameObject.AddComponent<AudioSource>();


	
	}
	
	// Update is called once per frame
	void Update () {

	}
	public void textura(string text){
		//print ("llegue: " + text);
		gameObject.renderer.material.mainTexture = Resources.Load (text+"") as Texture2D;
	}
	void OnMouseOver () {
		/*Cuando pase la mano por sobre la carta, deberia mostrar una version agrandada*/
		//print("hola");
		if (estadomouse == 1) {
			estadomouse=0;
			audioSource.clip = Resources.Load("sonidos/up") as AudioClip;
			audioSource.volume = 2f;
			audioSource.Play();
		}

		if(sube){

			sube=false;
			//generar la descripcion de la carta en el centro del campo
			if(controller.playerID==1)	
				descripcion=(GameObject)Instantiate(descr,new Vector3(11.6f,1f,-2f),descr.transform.rotation);
			else{
				descripcion=(GameObject)Instantiate(descr,new Vector3(-11.6f,-1f,-2f),descr.transform.rotation);
				descripcion.transform.rotation=Quaternion.Euler (-90,0,0);
			}
			descripcion.renderer.material.mainTexture=gameObject.renderer.material.mainTexture;
		}
		
		
		
		
	}
	
	void OnMouseExit(){
		if (estadomouse == 0) {
			estadomouse=1;
		
		}
		if(!sube) {

			sube=true;
			Destroy(descripcion);
		}
		
	}
	IEnumerator OnMouseDown(){
		if (controller.playerID == 1) {
			if(controller.fase==2 && auxG){
				auxG = false;
				controller.pegar();
				audioSource.clip = Resources.Load("sonidos/golpe") as AudioClip;
				audioSource.volume = 2f;
				audioSource.Play();
				yield return new WaitForSeconds (1f);
				auxG = true;
				print (controller.vida);

							
			}
			
		} else {
			if(controller.fase==5 && auxG){
				auxG = false;
				controller.pegar();
				audioSource.clip = Resources.Load("sonidos/golpe") as AudioClip;
				audioSource.volume = 2f;
				audioSource.Play();
				yield return new WaitForSeconds (1f);
				auxG = true;
				print (controller.vida);

			}
			
		}
	}
}
